package classLoader;

import java.io.File;

import utils.FileUtil;

public class LoadClass {
	public static Class<?> load(String scriptName) throws Exception{
		File file = new File("bin");
		if(!file.exists()){
			file.mkdir();
		}
		if(scriptName==null || "".equals(scriptName)) return null;
		String absPath = file.getAbsolutePath()+File.separatorChar+scriptName.replace("\\", "/");
		//截取包名
		String packageName = ClassUtils.getClassPackage(absPath.replace('.', '/') + ".java");
		
		//获得java文件内容
		String content = FileUtil.readStringFileUTF8(absPath.replace('.', '/') + ".java");
		
		System.out.println("compiler");
		boolean compilerSucceed = ScriptUtils.getInstance().compilerJavaCode(packageName+"."+scriptName, content);
		if(!compilerSucceed)
			return null;
		System.out.println("load");
		Class<?> clazz = ScriptUtils.getInstance().loadClass(packageName+"."+scriptName).get(0);
		return clazz;
	}
}
